package com.hwhhhh.graduationproject.service.impl;

import com.hwhhhh.graduationproject.controller.param.Lv1Req;
import com.hwhhhh.graduationproject.controller.param.Lv2Req;
import com.hwhhhh.graduationproject.dao.entity.CommentLv1Entity;
import com.hwhhhh.graduationproject.dao.entity.CommentLv2Entity;
import com.hwhhhh.graduationproject.dao.repo.CommentLv1Repo;
import com.hwhhhh.graduationproject.dao.repo.CommentLv2Repo;
import com.hwhhhh.graduationproject.domain.dto.CommentDTO;
import com.hwhhhh.graduationproject.domain.dto.CommentLv2DTO;
import com.hwhhhh.graduationproject.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description detail
 * Created by Hwhhhh on 2021/3/2 19:02
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentLv1Repo commentLv1Repo;
    @Autowired
    private CommentLv2Repo commentLv2Repo;

    /**
     * 按照发布时间获取教室评论区内容
     * @param classroomId   教室id
     * @param page  页码
     * @return
     */
    @Override
    public List<CommentDTO> getNewLv1ByClassroomId(int classroomId, int page) {
        // 页面大小为10
        Pageable pageable = PageRequest.of(page, 10);
        Page<CommentLv1Entity> lv1EntityPage =  this.commentLv1Repo.findAllByClassroomIdPageable(classroomId, pageable);
        List<CommentLv1Entity> lv1EntityList = lv1EntityPage.getContent();
        return lv1EntityList.stream().map(lv1Entity -> {
            // 一级评论部分
            CommentDTO commentDTO = new CommentDTO();
            BeanUtils.copyProperties(lv1Entity, commentDTO);
            // 二级评论部分, 有效字段为1
            List<CommentLv2Entity> lv2EntityList = this.commentLv2Repo.findAllByRootIdAndIsEffective(lv1Entity.getId(), 1);
            //二级评论中，需要重新设置相关数据
            List<CommentLv2DTO> lv2DTOList = lv2EntityList.stream().map(lv2Entity -> {
                CommentLv2DTO commentLv2DTO = new CommentLv2DTO();
                // lv2中自带的字段赋值
                BeanUtils.copyProperties(lv2Entity, commentLv2DTO);
                // 设置DTO中被回复者的相关信息，根据pid获得所回复人的信息
                CommentLv2Entity resp = this.commentLv2Repo.getOne(lv2Entity.getPid());
                commentLv2DTO.setRespId(resp.getUid());
                commentLv2DTO.setRespName(resp.getUname());
                return commentLv2DTO;
            }).collect(Collectors.toList());
            //将二级评论赋给一级评论并返回
            commentDTO.setLv2DTOS(lv2DTOList);
            return commentDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public int makeCommentLv1(Lv1Req lv1Req) {
        CommentLv1Entity entity = new CommentLv1Entity();
        BeanUtils.copyProperties(lv1Req, entity);
        return this.commentLv1Repo.save(entity).getId();
    }

    @Override
    public int makeCommentLv2(Lv2Req lv2Req) {
        CommentLv2Entity entity = new CommentLv2Entity();
        BeanUtils.copyProperties(lv2Req, entity);
        return this.commentLv2Repo.save(entity).getId();
    }

    @Override
    public void deleteLv1(int id) {
        CommentLv1Entity entity = this.commentLv1Repo.getOne(id);
        entity.setIsEffective(0);
        this.commentLv1Repo.save(entity);
    }

    @Override
    public void deleteLv2(int id) {
        CommentLv2Entity entity = this.commentLv2Repo.getOne(id);
        entity.setIsEffective(0);
        this.commentLv2Repo.save(entity);
    }
}
